From 0cb217e4942e1dfa7b4d19da3b5decb9f5dcdc09 Mon Sep 17 00:00:00 2001
From: Derek Lesho <dlesho@codeweavers.com>
Date: Fri, 3 Apr 2020 14:24:46 -0500
Subject: [PATCH] asfdemux: Always re-initialize metadata and global_metadata.

When transitioning from the PAUSED state, to the READY state, and back, metadata and global_metadata are left uninitialized, unlike when the demxuer transitions from NULL to READY, then to PAUSED.  I have found this to cause a segmentation fault when fields in these structures are set.
---
 gst/asfdemux/gstasfdemux.c | 26 ++++++++++++++++++++++----
 1 file changed, 22 insertions(+), 4 deletions(-)

diff --git a/gst/asfdemux/gstasfdemux.c b/gst/asfdemux/gstasfdemux.c
index 58c3019d..db7f6580 100644
--- a/gst/asfdemux/gstasfdemux.c
+++ b/gst/asfdemux/gstasfdemux.c
@@ -79,6 +79,7 @@ GST_STATIC_PAD_TEMPLATE ("video_%u",
 
 GST_DEBUG_CATEGORY (asfdemux_dbg);
 
+static void gst_asf_demux_finalize ( GObject * object );
 static GstStateChangeReturn gst_asf_demux_change_state (GstElement * element,
     GstStateChange transition);
 static gboolean gst_asf_demux_element_send_event (GstElement * element,
@@ -120,10 +121,14 @@ G_DEFINE_TYPE (GstASFDemux, gst_asf_demux, GST_TYPE_ELEMENT);
 static void
 gst_asf_demux_class_init (GstASFDemuxClass * klass)
 {
+  GObjectClass *gobject_class;
   GstElementClass *gstelement_class;
 
+  gobject_class = G_OBJECT_CLASS (klass);
   gstelement_class = (GstElementClass *) klass;
 
+  gobject_class->finalize = gst_asf_demux_finalize;
+
   gst_element_class_set_static_metadata (gstelement_class, "ASF Demuxer",
       "Codec/Demuxer",
       "Demultiplexes ASF Streams", "Owen Fraser-Green <owen@discobabe.net>");
@@ -216,10 +221,12 @@ gst_asf_demux_reset (GstASFDemux * demux, gboolean chain_reset)
     gst_caps_unref (demux->metadata);
     demux->metadata = NULL;
   }
+  demux->metadata = gst_caps_new_empty ();
   if (demux->global_metadata) {
     gst_structure_free (demux->global_metadata);
     demux->global_metadata = NULL;
   }
+  demux->global_metadata = gst_structure_new_empty ("metadata");
   if (demux->mut_ex_streams) {
     g_slist_free (demux->mut_ex_streams);
     demux->mut_ex_streams = NULL;
@@ -291,8 +298,6 @@ gst_asf_demux_reset (GstASFDemux * demux, gboolean chain_reset)
     demux->segment_running = FALSE;
     demux->keyunit_sync = FALSE;
     demux->accurate = FALSE;
-    demux->metadata = gst_caps_new_empty ();
-    demux->global_metadata = gst_structure_new_empty ("metadata");
     demux->data_size = 0;
     demux->data_offset = 0;
     demux->index_offset = 0;
@@ -4806,6 +4811,21 @@ gst_asf_demux_handle_src_query (GstPad * pad, GstObject * parent,
   return res;
 }
 
+static void gst_asf_demux_finalize (GObject * object)
+{
+  GstASFDemux *demux = GST_ASF_DEMUX(object);
+
+  if (demux->metadata)
+    gst_caps_unref (demux->metadata);
+  demux->metadata = NULL;
+
+  if (demux->global_metadata)
+    gst_structure_free (demux->global_metadata);
+  demux->global_metadata = NULL;
+
+  G_OBJECT_CLASS (parent_class)->finalize (object);
+}
+
 static GstStateChangeReturn
 gst_asf_demux_change_state (GstElement * element, GstStateChange transition)
 {
@@ -4820,8 +4840,6 @@ gst_asf_demux_change_state (GstElement * element, GstStateChange transition)
       demux->keyunit_sync = FALSE;
       demux->accurate = FALSE;
       demux->adapter = gst_adapter_new ();
-      demux->metadata = gst_caps_new_empty ();
-      demux->global_metadata = gst_structure_new_empty ("metadata");
       demux->data_size = 0;
       demux->data_offset = 0;
       demux->index_offset = 0;
-- 
2.24.1

